<?php
namespace Mobile\Controller;

use Common\Api\Wechat;
use Common\Controller\WeixinController;
use Think\Controller;

class WapController extends WeixinController
{
    protected $weobj;//实例化微信对象

    public function _initialize()
    {
        //获取公众号配置文件
        parent::_initialize();
        $this->weObj = new Wechat($this->config);

    }

    //用户授权
    public function authorization($scope = 'snsapi_base')
    {
        $callback = "http://" . $_SERVER["SERVER_NAME"] . "/Mobile/Wap/getuserinfo";
        $state = 'STATE';
        $wap_url = $this->weObj->getOauthRedirect($callback, $state, $scope);
        header("location:$wap_url");

    }

    //获取用户信息
    public function getuserinfo()
    {

        $data = $this->weObj->getOauthAccessToken($_GET);
        $openid = $data['openid'];
        $access_token = $data['access_token'];
        $userinfo = $this->weObj->getOauthUserinfo($access_token, $openid);
        if ($userinfo == false || $userinfo['errcode'] == 41008) {
            header("location:http://" . $_SERVER["HTTP_HOST"] . "/Mobile/Wap/authorization/scope/snsapi_userinfo");
        } else {
            $user_result = M('DealerUserinfo')->where("open_id='%s' and status=%d", [$openid, 1])->find();
            if ($user_result) {
                cookie('wap_openid', $openid, array('expire' => 3600 * 3600 * 24));
                cookie('wap_userId', $user_result['id'], array('expire' => 3600 * 3600 * 24));
                $this->redirect('ClientServer/personageCenter');
                exit;
            }
            if ($userinfo) {
                $user_data = $userinfo;
                S('userinfo', $userinfo, 200);
            } else {
                $user_data = S('userinfo');
            }
            if (empty($user_data)) {
                $this->error('数据失效,请重新授权', U('Wap/authorization'));
                exit;
            }
            $this->assign('userinfo', $user_data);
            $this->display('ClientServer/authorization');


        }

    }

    //用户登录
    public function login()
    {
        if (IS_POST) {
            $arr = D('DealerUserinfo')->userLogin($_POST);
            if ($arr['state'] == 'success') {
                $this->success($arr['info'], U('ClientServer/placeAnOrder'));
            } else {
                $this->error($arr['info']);
            }

        } else {
            $this->assign('title', '用户登录');
            $this->display('ClientServer/login');
        }

    }

    //用户注册
    public function register()
    {
        if (IS_POST) {
            $arr = D('DealerUserinfo')->bindingRegister($_POST);
            if ($arr['state'] == 'success') {
                $this->success($arr['info'], U('ClientServer/orderInquire'));
            } else {
                $this->error($arr['info']);
            }
        }
    }

    //头像上传
    public function touxiang()
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->savePath = './header_img/'; // 设置附件上传目录
        $upload->subName = '';
        $info = $upload->upload();
        if (!$info) {
            $arr['state'] = 'error';
            $arr['info'] = $upload->getError();

        } else {
            $up_img = $info['headimgurl'];
            $img = '/Uploads' . substr($up_img['savepath'], 1) . $up_img['savename'];
            $arr['state'] = 'success';
            $arr['info'] = "http://" . $_SERVER['HTTP_HOST'] . $img;
            $arr['url'] = $img;
        }
        echo json_encode($arr);
    }

    //退出登录
    public function outLogin()
    {
        cookie('wap_openid', null);
        cookie('wap_userId', null);
        $this->redirect('Wap/login');
    }

    //客服确认提交
    public function clientGet()
    {

        $rid = $_GET['rid'];
        $cid = M('Orders')->where("rid=%d", $rid)->getField('cid');
        if ($cid == 1) {
            $this->error('勿重复提交', '', IS_AJAX);
        } else {
            if (M('Orders')->where("rid=%d", $rid)->save(['cid' => 1])) {
                $order_number = M('Orders')->where("rid=%d", $rid)->getField('order_number');
                $data['url'] = U('Admin/Orders/toBeConfirmed');
                $data['rid'] = $rid;
                $data['sr_time'] = time();
                $data['content'] = "【客户已确认】订单号:" . $order_number . "请尽快查看处理!";
                D('Inform')->add($data);
                $this->success('提交成功', '', IS_AJAX);
            } else {
                $this->erro('提交失败', '', IS_AJAX);
            }
        }

    }


}


















